using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Xml;

using Pampa.WebControls;
using Pampa.CRUD.UI.CustomControls;

namespace Pampa.CRUD.Configuration
{
    public class DateCombosItem : EditItem
    {
        DatePickerCombos datePicker = new DatePickerCombos();

        int yearFrom;
        int yearTo;

        public DateCombosItem() : base()
        {
            this.yearFrom = 1940;
            this.yearTo = DateTime.Today.Year;
        }

        public DateCombosItem(DateCombosItem sourceItem) : base(sourceItem)
        {
            this.yearFrom = sourceItem.yearFrom;
            this.yearTo = sourceItem.YearTo;
        }

        #region Properties

        public override Type ValueTypeDefault
        {
            get { return typeof(System.DateTime); }
        }

        public override ItemType ItemType
        {
            get { return ItemType.DateCombosPicker; }
        }

        public int YearFrom
        {
            get { return yearFrom; }
            set { yearFrom = value; }
        }

        public int YearTo
        {
            get { return yearTo; }
            set { yearTo = value; }
        }

        public override Control Control
        {
            get
            {
                datePicker.CssClass = "datePickerCombos";
                datePicker.Enabled = this.Enabled;
                datePicker.YearFrom = this.YearFrom;
                datePicker.YearTo = this.YearTo;

                if (this.Value != null)
                    datePicker.Date = DateTime.Parse(this.Value.ToString());

                return datePicker;
            }
        }

        #endregion

        #region Public Functions

        public override VisibleItem Clone()
        {
            return new DateCombosItem(this);
        }

        public override void LoadXml(XmlNode node)
        {
            try
            {
                base.LoadXml(node);

                if (node.Attributes["YearFrom"] != null)
                    this.YearFrom = int.Parse(node.Attributes["YearFrom"].Value);

                if (node.Attributes["YearTo"] != null)
                    this.YearTo = int.Parse(node.Attributes["YearTo"].Value);
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Item " + this.Id + " mal definido.", ex);
            }
        }

        #endregion
    }
}
